<?php 
/**
 * 日志管理
 * @author Administrator
 *
 */
class logManage extends CControllerManageBase{
	public function show(){
		$model = new syslogModel();
		$pageIndex = $this->getRequestData("page","GET",1);
		$pageSize = 10;
		$c = 0;
		
		$s_time=$this->getRequestData("txt_s_time","get",date("Y-m-d"));
		$e_time=$this->getRequestData("txt_e_time","get",date("Y-m-d"));
		
		$e_time_old = $e_time;
		$e_time = date("Y-m-d",strtotime($e_time." +1 day"));
		
		$createUser = $this->getRequestData("txt_create_user","get");

		$urlParam["txt_s_time"] = $s_time;
		$urlParam["txt_e_time"] = $e_time_old;
		
		$where = "is_change=1 and module_identity<>'index' and right_name<>'show' and right_name<>'getCommunityByCity' and create_time>to_date('".$s_time."','yyyy-mm-dd hh24:mi:ss')";
		$where .= " and create_time<=to_date('".$e_time."','yyyy-mm-dd hh24:mi:ss')";
		
		if($createUser)
		{
			$where .= " and create_user='".$createUser."'";
			$urlParam["txt_create_user"] = $createUser;
		}
		
		$this->setViewParam("s_time", $s_time);
		$this->setViewParam("e_time", $e_time_old);
		$this->setViewParam("create_user", $createUser);
		
		$pageUrl = $this->getUrlParam($urlParam, array("txt_card","txt_owner_name"));
		$pageUrl = $pageUrl? "?".$pageUrl:"";
		
		$list = $model->selectPage($pageIndex,$pageSize,"syslog_id,create_user,create_time,ip,module_identity,right_name",$where,"syslog_id desc","",$c);

		$pageStr = CPage::showPage($pageIndex,$pageSize,$c,$pageUrl);
		
		if(count($list))
		{
			$moduleList = $model->selectDataSetByWhere(
				"tbl_base_module",
				"module_id,module_name,identity_name",array(),
				"is_show=1");
			
			$moduleRightList = $model->selectDataSetByWhere(
				"tbl_base_module_right",
				"module_id,right_ename,right_name"
			);
			
			foreach($list as &$item)
			{
				$arr = $this->getModuleName($item["module_identity"],$item["right_name"],$moduleList,$moduleRightList);
				
				$item["module_name"] = $arr[0];
				$item["right_name"] = $arr[1];
				
				/*$initDescription=json_decode($item['description'],true);
				//var_dump($initDescription);exit;
	           
	            if(empty($initDescription))
	            {
	            	continue;
	            }
	            else
	            {
	            	$description='<table width="330px">';
	                foreach($initDescription as $k1=>$v1)
	                {
	                	if($k1=="cookie")
	                	{
	                		continue;
	                	}
	                	$description.='<tr><td colspan="2"><div style="background:#ccc">'.$k1.'</div></td></tr><tr><td width="100">键</td><td>值</td></tr>';
		                foreach($v1 as $k2=>$v2)
		                {
		                	$v2 = $this->opDesc($v2);
		                    $description .='<tr><td>'.$k2.'</td><td>'.$v2.'</td></tr>';                 
		                }
	              	}
	              	$description .='</table>';   
	           }
	           
	           $item["description"] = $description;*/
			}
		}
		
		$this->setJsFile(array("/My97DatePicker/WdatePicker.js"));
		$this->setMenu("日志列表");
		$this->render("log/list",array("list"=>$list,"pageStr"=>$pageStr));
	}
	
	public function logDetail()
	{
		$id = $this->getRequestData("id","GET");
		$model = new syslogModel();
		$modelInfo = $model->selectById($id,"","create_user,create_time,ip,module_identity,right_name,description");
		if(!$modelInfo) {
			$this->sysMsg("记录不存在",1);
		}
		
		$moduleList = $model->selectDataSetByWhere(
			"tbl_base_module",
			"module_id,module_name,identity_name",array(),
			"is_show=1");
		
		$moduleRightList = $model->selectDataSetByWhere(
			"tbl_base_module_right",
			"module_id,right_ename,right_name"
		);
		
		$arr = $this->getModuleName($modelInfo["module_identity"],$modelInfo["right_name"],$moduleList,$moduleRightList);
				
		$modelInfo["module_name"] = $arr[0];
		$modelInfo["right_name"] = $arr[1];
		
		$initDescription=json_decode($modelInfo['description'],true);
		$description = "";
		if(!empty($initDescription))
		{
			$description='<table width="100%">';
			foreach($initDescription as $k1=>$v1)
			{
				if($k1=="cookie")
				{
					continue;
				}
				$description.='<tr><td colspan="2"><div style="background:#ccc">'.$k1.'</div></td></tr><tr><td width="100">键</td><td>值</td></tr>';
				foreach($v1 as $k2=>$v2)
				{
					if(is_array($v2))
					{
						$v2 = json_encode($v2);
					}
					$description .='<tr><td>'.$k2.'</td><td>'.$v2.'</td></tr>';                 
				}
			}
			$description .='</table>';   
		}
           
		$modelInfo['description'] = $description;
		$this->setMenu(array(
            array('text'=>'日志列表','href'=>"javascript:history.go(-1);"),
            array('text'=>"详细数据")
        ));
		$this->render("log/detail",array("modelInfo"=>$modelInfo));
	}
	
	private function opDesc($v)
	{
		if(is_array($v))
		{
			$v = json_encode($v);
		}

		if(preg_match("/^[\[\]\",\d]+$/",$v))
		{
			if(strlen($v)>30)
			{
				$v = $this->cutDesc($v,0);
			}
		}
		
		return $v;
	}
	
	private function cutDesc($v,$i)
	{
		$temp_l = substr($v,30*$i,30*($i+1));
		$temp_r = substr($v,30*($i+1));
		if(strlen($temp_r)>30)
		{
			return $this->cutDesc($temp_r,$i+1);
		}
		else{
			return $temp_l." ".$temp_r;
		}
	}
	
	private function getModuleName($moduleIdentityName,$action,$moduleList,$moduleRightList)
	{
		$moduleID = 0;
		$moduleName = "";
		$rightName = "";
		foreach($moduleList as $mItem)
		{
			if($mItem["identity_name"]==$moduleIdentityName)
			{
				$moduleName = $mItem["module_name"];
				$moduleID = $mItem["module_id"];
				break;
			}
		}

		foreach($moduleRightList as $rItem)
		{
			if($rItem["right_ename"]==$action && $rItem["module_id"]==$moduleID)
			{
				$rightName = $rItem["right_name"];
				break;
			}
		}
		
		return array($moduleName, $rightName);
	}
}
?>
